<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 客户管理API
 * @author zxj
 *
 */
class Customers extends CI_Controller {
	private $t_customers = 'mt_customers';
	function __construct() {
		parent::__construct();
	}


    /**
     * SF创建客户
     */
    function add(){
        $data = @file_get_contents('php://input');
        if(!$data){
            json_output($data,'未接受到数据,参数错误');
        }
        log_output($data);
        $customers = json_decode($data,true);
        $customerId = $customers['customerNum'];
        $type = $customers['recordType']=='Merchant Customer'?1:2;
        $indata = array(
            'name'=>$customers['name'],
            'type'=>$type,
            'tel'=>$customers['phone'],
            'email'=>$customers['personEmail'],
        );
        //更新
        if($customerId){
            $id = $this->common_model->update('mt_customers',$indata,array('id'=>$customerId));
        }else{
            $id = $this->common_model->insert('mt_customers',$indata);
        }
        if($id){
            echo json_encode(array('sfId'=>$customers['Id'],'omsId'=>$id,'msg'=>'操作成功','status'=>'1'));
        }else{
            echo json_encode(array('sfId'=>$customers['Id'],'omsId'=>$id,'msg'=>'操作失败','status'=>'0'));
        }
        exit;
    }


    /**
     * 客户列表
     */
    function index()
    {
        //输出数据结构
        $status = 0;
        $gpData = gp();
        //接受参数
        $where = $wherein = array();
        $type = $gpData['type'];
        if ($type){
            if (is_numeric($type)) {
                $where['type'] = $type;
            } else {
                $wherein['type'] = json_decode($type,true);
            }
        }
        //#分页
        $page = (int)$gpData['page'];
        $pagesize = (int)$gpData['pagesize'];
        $page = max($page, 1);
        $pagesize = empty($pagesize) ? $pagesize = 20 : $pagesize;
        $start_limit = ($page - 1) * $pagesize; //显示偏移
        $total_rows = $this->common_model->countRows($this->t_customers, $where, $wherein);//总数量
        $pagecount = ceil($total_rows / $pagesize); //总页数
        //查询返回的数组
        $dataList = $this->common_model->select($this->t_customers, $start_limit, $pagesize, 'id desc', $where, $wherein);
        //重构数据
        foreach ($dataList as $key => &$rs) {
            $rs['logo'] = '';
            $rs['add_time'] = date('Y-m-d H:i:s', $rs['add_time']);
            if($rs['id'] == 35){
               $rs['id'] = 0;
            }
        }
        unset($rs);
        //构造data数组
        $data = array(
            'page' => $page,
            'pagesize' => $pagesize,
            'pagecount' => $pagecount,
            'totalrows' => $total_rows,
            'dataList' => $dataList
        );
        //json判断处理
        if (empty($dataList)) {
            $message = 'No more data!';
        } else {
            $message = 'successfully.';
            $status = 1;
        }
        json_output($data, $message, $status);
    }



    /**
     * 获取单个客户信息
     */
    function get_customers_one()
    {
        //判断处理输出
        $data = array();
        $status = 0;
        $id = $this->input->get_post('id');
        $where = array('id'=>$id);
        //#客户
        $customerFirst = $this->common_model->selectFirst($this->t_customers, $where);
        if (!$customerFirst) {
            json_output($id, 'customer not exist', $status);
        }
        $message = 'successful';
        $status = 1;
        json_output($data, $message, $status);

    }


}
